import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 1000,
    students: [{
        name: 'zhnagssan',
        age: 20
      },
      {
        name: 'lisi',
        age: 30
      },
      {
        name: 'wangwu',
        age: 15
      },
      {
        name: 'lier',
        age: 25
      },
    ],
    info: {
      name: 'kobe',
      age: 20,
      address: 'codewhy'
    }
  },
  mutations: {
    increment(state) {
      state.count++
    },
    substract(state) {
      state.count--
    },
    updateinfo(state) {
      state.info.name = "dongbiao",
        state.info.age = 35
    }
  },
  actions: {
    //content上下文的意思

    aupdateinfo(content) {
      setTimeout(() => {
        content.commit('updateinfo')
      }, 1000)
    }

  },
  getters: {
    more20age(state) {
      return state.students.filter(s => s.age > 20)
    },
    more20agelength(state, getters) {
      return getters.more20age.length
    },
    morAgeStu(state) {
      return function (age) {
        return state.students.filter(s => s.age > age)
      }
    }
  },
  modules: {

  }

})

export default store
